/*
 * Copyright (c) 2018 maoyan.com
 * All rights reserved.
 *
 */
package com.darren.euc.util;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.ConvolveOp;
import java.awt.image.Kernel;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

/**
 * 在这里编写类的功能描述
 *
 * @author fangwenhao
 * @created 2018/3/16
 */
public class PicUtil {

    public static void resize(File originalFile, File resizedFile,double scale, float quality) throws IOException {
        ImageIcon ii = new ImageIcon(originalFile.getCanonicalPath());
        Image i = ii.getImage();
        int iWidth = (int) (i.getWidth(null)*scale);
        int iHeight = (int) (i.getHeight(null)*scale);
        //在这你可以自定义 返回图片的大小 iWidth iHeight
        Image resizedImage = i.getScaledInstance(iWidth,iHeight, Image.SCALE_SMOOTH);
        // 获取图片中的所有像素
        Image temp = new ImageIcon(resizedImage).getImage();
        // 创建缓冲
        BufferedImage bufferedImage = new BufferedImage(temp.getWidth(null),
                temp.getHeight(null), BufferedImage.TYPE_INT_RGB);
        // 复制图片到缓冲流中
        Graphics g = bufferedImage.createGraphics();
        // 清除背景并开始画图
        g.setColor(Color.white);
        g.fillRect(0, 0, temp.getWidth(null), temp.getHeight(null));
        g.drawImage(temp, 0, 0, null);
        g.dispose();
        // 柔和图片.
        float softenFactor =0.05f;
        float[] softenArray = { 0, softenFactor, 0, softenFactor,
                1 - (softenFactor * 4), softenFactor, 0, softenFactor, 0 };
        Kernel kernel = new Kernel(3, 3, softenArray);
        ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
        bufferedImage = cOp.filter(bufferedImage, null);
        FileOutputStream out = new FileOutputStream(resizedFile);
        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
        JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bufferedImage);
        param.setQuality(quality, true);
        encoder.setJPEGEncodeParam(param);
        encoder.encode(bufferedImage);
        bufferedImage.flush();
        out.close();
    }
    
    public static void resizeWidthFixed(File originalFile, File resizedFile,int width, float quality) throws IOException {
        ImageIcon ii = new ImageIcon(originalFile.getCanonicalPath());
        Image i = ii.getImage();
        int oldWidth = i.getWidth(null);
        if(oldWidth <= width){
		  FileChannel inputChannel = null;
	      FileChannel outputChannel = null;
          try {
              inputChannel = new FileInputStream(originalFile).getChannel();
              outputChannel = new FileOutputStream(resizedFile).getChannel();
              outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
          } finally {
              inputChannel.close();
              outputChannel.close();
          }
          return ;
        }
        double scale = (double)width/(double)oldWidth;
        int iWidth = (int) (i.getWidth(null)*scale);
        int iHeight = (int) (i.getHeight(null)*scale);
        //在这你可以自定义 返回图片的大小 iWidth iHeight
        Image resizedImage = i.getScaledInstance(iWidth,iHeight, Image.SCALE_SMOOTH);
        // 获取图片中的所有像素
        Image temp = new ImageIcon(resizedImage).getImage();
        // 创建缓冲
        BufferedImage bufferedImage = new BufferedImage(temp.getWidth(null),
                temp.getHeight(null), BufferedImage.TYPE_INT_RGB);
        // 复制图片到缓冲流中
        Graphics g = bufferedImage.createGraphics();
        // 清除背景并开始画图
        g.setColor(Color.white);
        g.fillRect(0, 0, temp.getWidth(null), temp.getHeight(null));
        g.drawImage(temp, 0, 0, null);
        g.dispose();
        // 柔和图片.
        float softenFactor =0.05f;
        float[] softenArray = { 0, softenFactor, 0, softenFactor,
                1 - (softenFactor * 4), softenFactor, 0, softenFactor, 0 };
        Kernel kernel = new Kernel(3, 3, softenArray);
        ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
        bufferedImage = cOp.filter(bufferedImage, null);
        FileOutputStream out = new FileOutputStream(resizedFile);
        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
        JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bufferedImage);
        param.setQuality(quality, true);
        encoder.setJPEGEncodeParam(param);
        encoder.encode(bufferedImage);
        bufferedImage.flush();
        out.close();
    }

}
